﻿//======================================================================
//        Copyright (C) 2010-2020 Yubq    
//        All rights reserved Yubq
//
//        Filename :CookiePer
//        Description : 
//        Email:yuhacker@126.com
//
//        Created by Yubq at  2011-1-6    
//======================================================================
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using eBlog.Common.Files;

namespace eBlog.Common
{
  public  class CookiePer
    {
        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <param name="strValue">值</param>
        /// <param name="strValue">过期时间(分钟),0为永久</param>
        public static void SetCookie(string strName, string strValue, int expires)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
            if (cookie == null)
            {
                cookie = new HttpCookie(strName);
            }

            cookie.Value = HttpContext.Current.Server.UrlEncode(strValue);
            //if (expires > 0)
            //{
            cookie.Expires = DateTime.Now.AddMinutes(expires);
            //}
            //else
            //{
            //    cookie.Expires = DateTime.Now.AddYears(10);
            //}
            HttpContext.Current.Response.AppendCookie(cookie);

        }

        /// <summary>
        /// 读cookie值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <returns>cookie值</returns>
        public static string GetCookie(string strName)
        {
            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
            {
                return HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.Cookies[strName].Value);
            }
            return string.Empty;
        }


    }
}
